package ywpt.web.action.people;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.PeopleEducation;
import ywpt.service.people.EducationService;
import ywpt.web.common.CommonDictionary;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class EducationAction extends BaseAction implements
ModelDriven<PeopleEducation>, Preparable{

	/**
	 * 教育情况
	 */
	private static final long serialVersionUID = -4329355785334640606L;
	private static int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	private Long peopleId;
	
	private PeopleEducation educationInfo;
	private List<PeopleEducation> educationInfoList = new  ArrayList<PeopleEducation>();
	private EducationService educationService ;
	
	
	public Long getPeopleId() {
		return peopleId;
	}
	public void setPeopleId(Long peopleId) {
		this.peopleId = peopleId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public PeopleEducation getEducationInfo() {
		return educationInfo;
	}
	public void setEducationInfo(PeopleEducation educationInfo) {
		this.educationInfo = educationInfo;
	}

	public List<PeopleEducation> getEducationInfoList() {
		return educationInfoList;
	}
	public void setEducationInfoList(List<PeopleEducation> educationInfoList) {
		this.educationInfoList = educationInfoList;
	}
	public EducationService getEducationService() {
		return educationService;
	}

	public void setEducationService(EducationService educationService) {
		this.educationService = educationService;
	}
	//***********************************************************************
	@Override
	public PeopleEducation getModel() {
		return this.educationInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null)
		{
			this.educationInfo = this.educationService.getEducationInfoById(id);
		}
		else
		{
			this.educationInfo = new PeopleEducation();
		}
	}
//******************************************************************************	
	
	public String list() throws Exception{
		DetachedCriteria query = DetachedCriteria.forClass(PeopleEducation.class);
		query.createAlias("people", "people").add(Restrictions.eq("people.id", peopleId));
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = educationService.getEducationInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		educationInfoList.addAll(educationService.getEducationInfos(query, pageSize,
				rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		return SUCCESS;
	}

	public String input() 
	{
		this.setPeopleId(peopleId);
		return INPUT;
	}
	public String edit() 
	{
		educationInfo=this.educationService.getEducationInfoById(id);
		this.setPeopleId(educationInfo.getPeople().getId());
		return INPUT;
	}
	/**
	 * ����
	 * 
	 * @return
	 */
	public String save() throws Exception {	

		try {
			if (id == null) 
			{
				educationService.saveEducationInfo(educationInfo,peopleId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
				return SUCCESS;
			} 
			else 
			{   				
				educationService.updateEducationInfo(educationInfo,peopleId);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
				return SUCCESS;					
			}
		} 
		catch (UniqueConstraintException e) 
		{	
			this.addActionError(e.getMessage());
			return INPUT;
		}
	}	
	/**
	 * 
	 * @return
	 */
	public String delete() throws Exception {		
		if (id != null) 
		{	
			educationService.removeEducationInfo(id);
			return SUCCESS;
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
		
	}	
	
	

}
